/*           
Module : PSTAT.CPP
Purpose: Provides a generalised framework for executing a lengthy operation in a thread. Feedback is provided 
         by a progress dialog which can optionally be cancelled.
Created: 27-03-1997

Copyright (c) 1997 - 2003 by PJ Naughter.  (Web: www.naughter.com, Email: pjna@naughter.com)

All rights reserved.

Copyright / Usage Details:

You are allowed to include the source code in any product (commercial, shareware, freeware or otherwise) 
when your product is released in binary form. You are allowed to modify the source code in any way you want 
except you cannot modify the copyright details at the top of each module. If you want to distribute source 
code with your application, then you are only allowed to distribute versions released by the author. This is 
to maintain a single distribution point for the source code. 

*/


////////////////////////////// Defines /////////////////////////////////////////////

#ifndef __PSTAT_H__
#define __PSTAT_H__


#ifndef __AFXMT_H__
#pragma message("To avoid this message please put afxmt.h into your PCH (normally stdafx.h)")
#include <afxmt.h>
#endif
#include "afxwin.h"


//// Flags which can be used to customize the Pstat code
const DWORD PSTAT_CANCEL        = 0x0001;  // Display a cancel button on the progress dialog
const DWORD PSTAT_CONFIRMCANCEL = 0x0002;  // Confirm cancel by displaying a messagebox
                                              

///// forward declaration
class CProgressThreadDlg;

///// The function declaration of the function to be called in a secondary thread with a progress dialog
typedef ULONG (FUNCTION_WITH_PROGRESS)(void* pData, CProgressThreadDlg* pProgressDlg);



////////////////////////////// Classes ////////////////////////////////////////////

class CProgressThreadDlg : public CDialog
{
public:
  DECLARE_DYNAMIC(CProgressThreadDlg)

// Constructors / Destructors
	CProgressThreadDlg(BOOL bShowCancelButton, BOOL bConfirmCanel, CWnd* pParent = NULL);

// Methods
  void Close();  
  BOOL HasBeenCancelled() const { return m_bCancelled; };
  void SetPercentageDone(int Percentage);


/* Actual function to call to get your function to work in a separate thread 
   with a progress dialog being displayed while it is executing

   Description of parameters taken are:

   pfnFunction:    Your function to execute.
   sProgressTitle: The title that should appear on the progress dialog while your function is executing.
   pData:          Any user defined data that your function will be called with. Similar to the way threads give
                   you a single LPVOID parameter.
   dwFlags:        Bitwise compliment of flags to customize how the function will be executed. Currently only
                   PSTAT_CANCEL and PSTAT_CONFIRMCANCEL are available which will display a cancel button on the progress dialog.
   sConfirmPrompt: Message text to display in confirm cancel message if PSTAT_CONFIRMCANCEL flag is used
   nPriority:      The thread priority of the thread pfnFunction will execute at. See Win32 help for further
                   details if unclear.
   pParent:        The window which the progress dialog will use as its parent.

   Return value:   TRUE if the function was successfully executed otherwise FALSE i.e. the dialog was cancelled 
                   or some other error occurred.
*/
  friend BOOL ExecuteFunctionWithProgressDialog(FUNCTION_WITH_PROGRESS* pfnFunction, const CString& sProgressTitle, void* pData = NULL, 
                                                DWORD dwFlags = 0, const CString& sConfirmPrompt = _T(""), int nPriority = THREAD_PRIORITY_NORMAL, CWnd* pParent = NULL);

protected:

	

	//{{AFX_DATA(CProgressThreadDlg)
	enum { IDD = IDD_PROGRESS };
	CProgressCtrl	m_ctrlProgress;
	//}}AFX_DATA

  virtual void OnCancel();
  virtual void OnOK();

	//{{AFX_VIRTUAL(CProgressThreadDlg)
	virtual void DoDataExchange(CDataExchange* pDX);
	//}}AFX_VIRTUAL

	//{{AFX_MSG(CProgressThreadDlg)
	virtual BOOL OnInitDialog();
	afx_msg void OnCancelled();
	//}}AFX_MSG
	afx_msg LRESULT OnFinishSearch(UINT, LONG);

		DECLARE_MESSAGE_MAP()



  CTime       m_TimeCreation;
  int         m_nCurrentPercentage;
  BOOL        m_bReady;
  CString     m_sCaption;
  CString     m_sConfirmPrompt;
  BOOL        m_bShowCancelButton;
  BOOL        m_bConfirmCancel;
  BOOL        m_bCancelled;
  BOOL        m_bOkToClose;
  CWinThread* m_pThread;
  CEvent*     m_pDialogInitCompleted;
public:
	
	CStatic ProgressMessageCtrl;
};

#endif //__PSTAT_H__
